package com.sunny.framework.system.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.framework.core.base.action.BaseAction;
import com.sunny.framework.system.entity.Role;
import com.sunny.framework.system.service.RoleService;

@Controller
@RequestMapping("system/role.html")
public class RoleAction extends BaseAction<Role>{

	private Logger logger = Logger.getLogger(RoleAction.class);
	
	@Autowired
	private RoleService service;
	
	@RequestMapping(params="save")
	@Override
	public @ResponseBody Map<String, Object> save(Model model, HttpServletRequest request,
			HttpServletResponse response, Role t) {
		logger.info("save");
		try {
			service.save(t);
			super.map_data();
		} catch (Exception e) {
			super.map_eror(e);
		}
		return map;
	}

	@RequestMapping(params="listJson")
	@Override
	public @ResponseBody Map<String, Object> listJson(Model model,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("listJson");
		
		try {
			super.pager = pager(request);
		
			String name = request.getParameter("name");
			String dis = request.getParameter("dis");
			if(name!=null&&!name.isEmpty())
				criterions.add(Restrictions.ilike("name", name));
			if(dis!=null&&!dis.isEmpty())
				criterions.add(Restrictions.eq("dis", dis));
			
			super.map_pager(service.find(criterions, pager.getFirstResults(),
					pager.getMaxResults()), service.num(criterions));
		} catch (Exception e) {
			logger.error(e);
		}
		return map;
	}

	@RequestMapping(params="list")
	@Override
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("list");
		return "system/ListRole";
	}

	@Override
	public String add(HttpServletRequest reuqest, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	@RequestMapping(params="edit")
	@Override
	public @ResponseBody Map<String, Object> edit(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("edit");
		try {
			super.map_data(service.findById(Long.parseLong(request.getParameter("id"))));
		} catch (Exception e) {
			super.map_eror(e);
		}
		return map;
	}

	@RequestMapping(params="delete")
	@Override
	public @ResponseBody Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("delete");
		try {
			service.deleteByIds(super.stringToLongArray(request.getParameter("id")));
			super.map_data();
		} catch (Exception e) {
			super.map_eror(e);
		}
		return map;
	}

}
